package com.smedi.ismedi.database.starter.util;

import java.sql.*;
/**
 * @author yangzhenzhou
 * @since 2023/1/12
 */
public class SimpleJdbcUtil {
    private static String driverClass;
    private static String url;
    private static String user;
    private static String password;


    public SimpleJdbcUtil(String driverClass,String url,String user,String password ) {
        SimpleJdbcUtil.driverClass = driverClass;
        SimpleJdbcUtil.url = url;
        SimpleJdbcUtil.user = user;
        SimpleJdbcUtil.password = password;

        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void close(Connection conn, PreparedStatement pre, ResultSet res) {
        if(res != null) {
            try {
                res.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(pre != null) {
            try {
                pre.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
